package de.koller.worlders.gameplay.gui;

import de.koller.oceanFramework.gl.util.IRenderableImage;
import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.plain.fullParticle.ParticleImage;
import de.koller.worlders.WSea;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.gameplay.ProgressUnit;
import de.koller.worlders.render.IGLRender;
import de.koller.worlders.render.util.TextureRep;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import org.lwjgl.opengl.GL11;

public class ProgressV extends WSea implements IGLRender {
	
	private boolean show = false;
	private float scroll = 0;
	
	private final int ttw = 350, tth = 120;
	private IRenderableImage plain;
	private ParticleTexture toolTip;
	private ParticleImage toolTipImg;

	private ProgressUnit lastToolTip = null;
	
	@Override
	protected void init() {
		super.init();
		
		plain = new TextureRep( 0xff404040 ).generate( wroot.getBuffers() );

		toolTipImg = new ParticleImage( ttw, tth );
		this.addChild( toolTipImg );
		
		toolTip = ParticleTexture.generate( ttw, tth );
		toolTip.clear( 0xff00ff00 );
		toolTip.flush();
	}
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
//		if( wroot.getInput().isKeyPressed( Keyboard.KEY_I ) && !wroot.getGui().getConsoleInput().isOpen() ) {
//			show = !show;
//			wroot.getDebug().setActive( show );
//		}
		
		if( !show )
			return;

		if( wroot.getInput().getDMouseWheel() > 0 )
			scroll += elapsedTime;
		if( wroot.getInput().getDMouseWheel() < 0 )
			scroll -= elapsedTime;
	}

	@Override
	public void renderGL( int elapsedTime ) {
		if( !show )
			return;
		
		GL11.glColor3f( 1, 1, 1 );
		int xs = (WorldersRoot.D_WIDTH - 64*9) / 2;
		
		for( int y = 0; y < 20; y++ )
			for( int i = 0; i < 9; i++ )
				plain.draw2D( i*64 + xs, y*64+scroll, 64, 64, 50 );
		
		int mx, my;
		mx = wroot.getInput().getMouseX();
		my = wroot.getInput().getMouseY();
		
		for( ProgressUnit u : wroot.gamePlayer.playerPoints.getProgress().getUnits() ) {
			if( u.getRimg() == null )
				u.setRimg( u.getTextureRep().generate( wroot.getBuffers() ) );
			
			if( !u.isAchieved() )
				GL11.glColor3f( 0.2f, 0.2f, 0.2f );
			else
				GL11.glColor3f( 1.0f, 1.0f, 1.0f );
			
			u.getRimg().draw2D( u.getX()*64 + 5 + xs, u.getY()*64 + 5 + scroll, 64 - 10, 64 - 10, 51 );
		
			GL11.glColor3f( 1.0f, 0.0f, 0.0f );
			if( !u.isAvailable() )
				plain.draw2D( u.getX()*64 + xs, u.getY()*64+scroll, 64, 64, 50.5f );
			
			if( mx < u.getX()*64 + xs )
				continue;
			if( my < u.getY()*64 + scroll )
				continue;
			if( mx > u.getX()*64 + 64 + xs)
				continue;
			if( my > u.getY()*64 + scroll + 64 )
				continue;
	
			GL11.glColor3f( 1.0f, 1.0f, 1.0f );
			
			if( u != lastToolTip )
				setTooltip( u );
			toolTip.draw2D( mx - ttw/2, my + 10, ttw, tth, 52 );
		}

	}
	
	private void setTooltip( ProgressUnit pu ) {
		toolTipImg.getPgraphics().clear( 0xff000000 );
		
		Graphics2D g = toolTipImg.getGraphics();
		g.setColor( Color.WHITE );
		g.setFont( new Font( Font.DIALOG, Font.BOLD, 15 ) );
		g.drawString( pu.achieveText()[0], 5, 18 );
		
		g.setColor( new Color( 0xffe0e0e0 ) );
		g.setFont( new Font( Font.DIALOG, Font.PLAIN, 12 ) );
		for( int i = 1; i < pu.achieveText().length; i++ )
			g.drawString( pu.achieveText()[i], 5, 23 + i*13 );
		
		g.setColor( new Color( 0xff90ff90 ) );
		g.setFont( new Font( Font.DIALOG, Font.PLAIN, 12 ) );
		for( int i = pu.achieveReward().length - 1; i >= 0; i-- )
			g.drawString( pu.achieveReward()[i], 5, tth - (pu.achieveReward().length-i)*13 );
		
		g.setColor( Color.WHITE );
		g.drawRect( 0, 0, ttw-1, tth-1 );
		
		for( int x = 0; x < toolTipImg.getBitmapWidth(); x++ )
			for( int y = 0; y < toolTipImg.getBitmapHeight(); y++ )
				toolTip.setPixel( toolTipImg.getPgraphics().colorAt( x, y ), x, y );
		toolTip.flush();
		lastToolTip = pu;
	}
	
}
